package cn.maihe.elg.operation.centers.epoint.handler.service;

import cn.maihe.elg.operation.centers.epoint.config.EpointCenterInfosConfig;
import cn.maihe.elg.operation.centers.epoint.dto.EpointBaseRespDTO;
import cn.maihe.elg.operation.centers.epoint.dto.handler.MeiShanGiveupApplyReq;
import cn.maihe.elg.operation.centers.epoint.handler.EpointCenterTradeService;
import cn.maihe.elg.operation.centers.supports.CenterTradeContext;
import cn.maihe.elg.operation.centers.supports.annotation.CenterTradeService;
import cn.maihe.elg.operation.model.enums.AcceptOrderStatus;
import cn.maihe.elg.operation.model.enums.CenterNoEnum;
import cn.maihe.elg.operation.model.enums.CenterProviderEnum;
import cn.maihe.elg.operation.model.enums.TradeType;
import cn.maihe.elg.operation.repository.entity.AcceptOrderInfoDO;
import cn.maihe.elg.operation.repository.entity.ProjectInfoDO;
import cn.maihe.elg.operation.repository.entity.RevokeApplayOrderInfoDO;
import cn.maihe.elg.operation.service.business.ProjectInfoService;
import cn.maihe.elg.operation.service.order.AcceptOrderInfoService;
import cn.maihe.elg.operation.utils.IdGenerate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * @Description 数据服务平台放弃申请
 * @Author SSX
 * @Date 2025/05/28
 */
@CenterTradeService(
        provider = CenterProviderEnum.EPOINT,
        centerNo = CenterNoEnum.MEISHAN,
        tradeType = TradeType.giveupapply,
        reqClass = MeiShanGiveupApplyReq.class,
        respClass = EpointBaseRespDTO.class
)
@Slf4j
public class MeiShanGiveupApplyService extends EpointCenterTradeService<MeiShanGiveupApplyReq, EpointBaseRespDTO> {
    @Resource
    private AcceptOrderInfoService acceptOrderInfoService;
    @Resource
    private ProjectInfoService projectInfoService;

    public MeiShanGiveupApplyService(EpointCenterInfosConfig centerInfosConfig) {
        super(centerInfosConfig);
    }

    /**
     * 具体业务处理
     *
     * @param context
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public EpointBaseRespDTO processService(CenterTradeContext<MeiShanGiveupApplyReq, EpointBaseRespDTO> context) {
        MeiShanGiveupApplyReq req = context.getReq();
        Long centerId = context.getCenterId();
        Long orgId = context.getOrgId();
        String applyno = req.getApplyno();

        AcceptOrderInfoDO acceptOrderInfoDO = acceptOrderInfoService.getByCenterOrderId(centerId, orgId, applyno);
        if (acceptOrderInfoDO == null) {
            return EpointBaseRespDTO.error("查无此投保申请记录");
        }

        if (acceptOrderInfoDO.getStatus() == AcceptOrderStatus.CANCEL.getStatus()) {
            return EpointBaseRespDTO.error("");
        }

        if (acceptOrderInfoDO.getStatus() == AcceptOrderStatus.HAS_OUT_GUARANTEE.getStatus()) {
            return EpointBaseRespDTO.error("已出函，不能放弃申请");
        }

        if (acceptOrderInfoDO.getStatus() == AcceptOrderStatus.FINISH.getStatus()) {
            return EpointBaseRespDTO.error("保函已完成，不能放弃申请");
        }

        AcceptOrderInfoDO updateOrderInfo = AcceptOrderInfoDO.builder().id(acceptOrderInfoDO.getId())
                .status(AcceptOrderStatus.CANCEL.getStatus()).remark("放弃申请关闭订单").build();



        RevokeApplayOrderInfoDO revokeApplayOrderInfoDO = RevokeApplayOrderInfoDO.builder()
                .id(IdGenerate.getId())
                .centerId(centerId)
                .orgId(orgId)
                .centerOrderId(req.getApplyno())
                .revokeNo(req.getApplyno())
                .status(1)
                .build();

        ProjectInfoDO project = projectInfoService.selectById(acceptOrderInfoDO.getProjectId());
        if(Objects.nonNull(project)){
            revokeApplayOrderInfoDO.setProjectCode(project.getProjectCode());
            revokeApplayOrderInfoDO.setProjectName(project.getProjectName());
            revokeApplayOrderInfoDO.setSectionCode(project.getSectionCode());
            revokeApplayOrderInfoDO.setSectionName(project.getSectionName());
            revokeApplayOrderInfoDO.setTenderer(project.getTenderer());
        }

        acceptOrderInfoService.revokeOrder(updateOrderInfo, revokeApplayOrderInfoDO);

        return EpointBaseRespDTO.success("放弃投保申请已完成");
    }
}
